<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ include file="header.jsp" %>

<%@ page import="com.wxt.blog.service.BlogService" %>
<%@ page import="com.wxt.blog.entity.Blog" %>
<%@ page import="java.util.List" %>

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
    // 获取博客列表
    BlogService blogService = new BlogService();
    List<Blog> blogs = blogService.findAllBlogs();
    request.setAttribute("blogs", blogs);
%>
    <h1>最新博客</h1>

    <div style="margin: 20px 0;">
        <form action="${pageContext.request.contextPath}/blog/search" method="get" style="display: flex;">
            <input type="text" name="keyword" placeholder="搜索博客..." style="flex: 1; padding: 8px;">
            <button type="submit" class="btn" style="margin-left: 10px;">搜索</button>
        </form>
    </div>

    <c:forEach items="${blogs}" var="blog">
        <div style="background: white; padding: 20px; margin-bottom: 20px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1);">
            <h2><a href="${pageContext.request.contextPath}/blog/detail?id=${blog.id}" style="color: #2c3e50; text-decoration: none;">${blog.title}</a></h2>
            <p style="color: #7f8c8d; margin: 10px 0;">作者: ${blog.authorName} | 发布于: <fmt:formatDate value="${blog.createdAt}" pattern="yyyy-MM-dd"/></p>
            <p style="margin: 10px 0;">
                ${fn:length(blog.content) > 100 ? fn:substring(blog.content, 0, 100) : blog.content}
                <c:if test="${fn:length(blog.content) > 100}">...</c:if>
            </p>
            <div style="display: flex; justify-content: space-between; margin-top: 15px;">
                <div>
                    <a href="${pageContext.request.contextPath}/blog/detail?id=${blog.id}" class="btn">阅读更多</a>
                </div>
                <div>
                    <span>浏览量: ${blog.viewCount}</span>
                </div>
            </div>
        </div>
    </c:forEach>

    <c:if test="${empty blogs}">
        <p style="text-align: center; margin: 50px 0;">暂无博客</p>
    </c:if>
<%@ include file="footer.jsp" %>